package com.warmlight.voicepacket.widget;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.ui.communitylm.widget.ReportDialog;
import com.warmlight.voicepacket.utils.ToastUtil;

public class ReportCopaPopWindow extends PopupWindow implements View.OnClickListener{
    private TextView copyContent;
    private View vLine;
    private TextView tvReportcontent;
    private float density = 1.0f;
    private Context mContext;
    private String id;
    private String content;

    public void setID(String id,String content){
       this.id=id;
       this.content=content;
       if (TextUtils.isEmpty(content)){
           vLine.setVisibility(View.GONE);
           copyContent.setVisibility(View.GONE);
       }
    }
    public ReportCopaPopWindow(Context context) {
        mContext=context;
        initPopupWindow();
        View view = View.inflate(context, R.layout.popwindow_roportcopy, null);


        copyContent = view. findViewById(R.id.copy_content);
        vLine =view. findViewById(R.id.v_line);
        tvReportcontent = view. findViewById(R.id.tv_reportcontent);
        tvReportcontent.setOnClickListener(this);
        copyContent.setOnClickListener(this);
        setContentView(view);
        //设置popwindow的宽高，这个数字是多少就设置多少dp，注意单位是dp
        setHeight((int) (55*density));
        setWidth((int) (120*density));
    }

    //初始化popwindow
    private void initPopupWindow() {
//        setAnimationStyle(R.style.popwindowAnim);//设置动画
        setFocusable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new BitmapDrawable());
        density = mContext.getResources().getDisplayMetrics().density;//
    }

    /**
     * 显示popupWindow
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
//            this.showAsDropDown(parent, parent.getLayoutParams().width / 2, -parent.getLayoutParams().width);
//            this.showPopupWindow(parent);
            int[] location = new int[2];
            parent.getLocationOnScreen(location);
            this.showAtLocation(parent, Gravity.NO_GRAVITY, location[0]/2-60, location[1]-this.getHeight());
        } else {
            this.dismiss();
        }
    }

    @Override
    public void onClick(View v) {
        //自己设置点击事件。。。
        switch (v.getId()){
            case R.id.tv_reportcontent:
                ReportDialog reportDialog = new ReportDialog();
                reportDialog.setID("2",id);
                reportDialog.show(((FragmentActivity)mContext).getSupportFragmentManager(),"report");
                dismiss();
                break;
            case R.id.copy_content:
                //获取剪贴板管理器：
                ClipboardManager cm = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                ClipData mClipData = ClipData.newPlainText("Label", content);
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                ToastUtil.show(mContext,"复制成功", Toast.LENGTH_SHORT);
                dismiss();
                break;
        }
    }
}